<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;

class Department extends Base
{
    use SoftDeletes;
    // 时间字段是否为时间戳格式
    protected $dateFormat = 'Y-m-d H:i:s';
    //

    protected $guarded = [];

    public function users(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
    {
        return $this->belongsToMany(User::class, 'user_department');
    }

    /** 查询部门列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        return $this->makeQuery($request)->paginate($request->input('limit', 10));
    }

    /** 查询部门列表组装查询条件
     * @param Request $request
     * @return mixed
     */
    public function makeQuery(Request $request): mixed
    {
        $query = $this->select('*');
        if ($request->has('name')) {
            $query->where('name', 'like', '%' . $request->input('name') . '%');
        }
        return $query;
    }

}
